package com.shop.cereshop.app.service.imreport.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import com.shop.cereshop.app.dao.imreport.ImReportMapper;
import com.shop.cereshop.app.service.imreport.ImReportService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.im.tx.req.user.SetGlobalBanReq;
import com.shop.cereshop.commons.domain.imReport.CereImReport;
import com.shop.cereshop.commons.domain.imReport.dto.CereImReportParam;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.service.im.ImUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ImReportServiceImpl implements ImReportService {

    @Autowired
    private ImReportMapper imReportMapper;
    @Autowired
    private    ImUserService imUserService;

    @Override
    public Page<CereImReport> getReportPage(CereImReportParam param) throws CoBusinessException {
        PageHelper.startPage(param.getPage(), param.getPageSize());

        List<CereImReport> list = imReportMapper.findByCondition(param);
        PageInfo<CereImReport> pageInfo = new PageInfo<>(list);

        return new Page<>(pageInfo.getList(), pageInfo.getTotal());
    }



    @Override
    public CereImReport getReportById(Long reportId) throws CoBusinessException {
        return imReportMapper.selectByPrimaryKey(reportId);
    }



    @Override
    public Result<Void> addReport(CereImReportParam param , CereBuyerUser user ) throws CoBusinessException {
        CereImReport record = new CereImReport();
        //前端传过来得 reportImages是List  处理成字符串 用,分割
        if(param.getReportImages() != null){
            String reportImages = String.join(",", param.getReportImages());
            record.setReportImages(reportImages);
        }
        record.setReportReason(param.getReportReason());
        record.setReportTargetId(param.getReportTargetId());
        record.setReportUserId(user.getBuyerUserId());
        record.setReportTime(new Date());
        record.setReportTypeId(param.getReportTypeId());
        int i = imReportMapper.addReport(record);
        if (i >0) {
            return new Result<>(CoReturnFormat.SUCCESS);
        }else{
            return new Result<>(CoReturnFormat.FAIL);
        }

    }

    @Override
    public Result<Void> updateReport(CereImReportParam param) throws CoBusinessException {
        CereImReport record = new CereImReport();
        BeanUtils.copyProperties(param, record);
        int i = imReportMapper.updateByPrimaryKeySelective(record);
        if (i >0) {
            return new Result<>(CoReturnFormat.SUCCESS);
        }else{
            return new Result<>(CoReturnFormat.FAIL);
        }


    }


}
